//


#import "MB_PersonalViewController.h"
#import "MB_PersonalTableViewCell.h"
#import "MB_TimeView.h"

@interface MB_PersonalViewController ()<UITableViewDelegate,UITableViewDataSource,MB_PersonalTableDelegate,MB_TimeViewDelegate>

/** */
@property (nonatomic, strong) UITableView *personalTableView;
/** name*/
@property (nonatomic, copy) NSString *namestr;
/** 生日*/
@property (nonatomic, copy) NSString *birthdaystr;
/** 兴趣*/
@property (nonatomic, copy) NSString *intereststr;
/** 座右铭*/
@property (nonatomic, copy) NSString *mottostr;
/** */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** */
@property (nonatomic, strong)  MB_SqlView *sql;
/** */
@property (nonatomic, strong) MB_TimeView *datePickerView;
@end

@implementation MB_PersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setnavBatalweeTitle:@"Personal"];
    
    [self.view addSubview:self.personalTableView];
    
    _sql = [[MB_SqlView alloc]initWithData:@"PersonalData" dataName:@"MoodData" success:^{} fail:^(NSError * _Nullable error) {}];
    [self getData];
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    NSArray *array = @[@"Nickname",@"Birthday",@"Interests",@"Motto"];
    if (indexPath.row == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = array[indexPath.row];
        cell.textLabel.textColor = rgba(90, 90, 90, 1);
        cell.detailTextLabel.text = _birthdaystr.length ==0 ? @"2010-11-33":_birthdaystr;
        return cell;
    }else {
        MB_PersonalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier3"];
        if (!cell) {
            cell = [[MB_PersonalTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier3"];
        }
        cell.index = indexPath.row;
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = array[indexPath.row];
        [cell setModel:_dataArray.firstObject];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.datePickerView mbshow];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kRHeight(100);
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, kMainScreenWidth - 100, 45)];
    [btn setTitle:@"Save" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:RGB(255, 106, 106)];
    btn.layer.cornerRadius = 8;
    [btn addTarget:self action:@selector(selector) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    return view;
}
- (void)selector {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:_namestr.length == 0 ?@"":_namestr forKey:@"nickname"];
    [dic setObject:_birthdaystr.length == 0 ?@"2010-10-10":_birthdaystr forKey:@"birthday"];
    [dic setObject:_intereststr.length == 0 ?@"":_intereststr forKey:@"interests"];
    [dic setObject:_mottostr.length == 0 ?@"":_mottostr forKey:@"motto"];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"one"] == nil) {
        [dic setObject:@"1" forKey:@"iD"];
        [_sql mb_insertNewEntity:dic success:^{
            [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"one"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }else{
        [_sql mb_updateEntity:dic key:@"iD = 1" success:^{
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }
    
}
//MARK:cellDelegate
- (void)sendCelltext:(NSString *)text index:(NSInteger)index {
    switch (index) {
        case 0:
            _namestr = text;
            break;
        case 2:
            _intereststr = text;
            break;
        case 3:
            _mottostr = text;
            break;
        default:
            break;
    }
}
//MARK:日期回调
- (void)mbchangeTime:(UIDatePicker *)datePicker {
    //
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate:datePicker.date];
    _birthdaystr = dateStr;
    //
    [_personalTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:0],nil] withRowAnimation:UITableViewRowAnimationNone];
}

//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)personalTableView {
    if (!_personalTableView) {
        _personalTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight) style:UITableViewStylePlain];
        _personalTableView.dataSource = self;
        _personalTableView.delegate   = self;
        _personalTableView.tableFooterView = [UIView new];
        _personalTableView.rowHeight  = kRHeight(50);
    }
    return _personalTableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}
- (MB_TimeView*)datePickerView {
    if (!_datePickerView) {
        _datePickerView = [[MB_TimeView alloc] init];
        _datePickerView.delegate = self;
        [_datePickerView mbsetDatePickerMode:UIDatePickerModeDate];
    }
    return _datePickerView;
}
//shuju
- (void)getData {
    
    [_sql mb_readEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        self.dataArray = [results copy];
        PersonalData *model = self.dataArray.firstObject;
        self.birthdaystr = model.birthday;
        [self.personalTableView reloadData];
        
    } fail:^(NSError * _Nullable error) {}];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
